package com.hdax.controller;

import com.hdax.entity.*;
import com.hdax.service.*;
import com.hdax.service.Impl.EmployeeInfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/demand")
public class DemandController {

    @Autowired
    private DemandService demandService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private EmployeeInfoService employeeInfoService;

    @Autowired
    private InterviewService interviewService;

    @Autowired
    private EmployeeInfoServiceImpl employeeInfoServiceimpl;

    @GetMapping(path = "/index")
    public String index(){
        return "demand/DemandProfiles";
    }

    @PostMapping(path = "/list")
    @ResponseBody
    public Map<String,Object> list(){
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        List<Demand> demands= demandService.findAll();
        for (int i = 0; i < demands.size(); i++) {
            int demand=demands.get(i).getDemand();
            String name=employeeInfoService.findEmployeeInfoById(demand).getEmpName();
            demands.get(i).setDemandname(name);
            int recruit =demands.get(i).getRecruit();
            String rename=employeeInfoService.findEmployeeInfoById(recruit).getEmpName();
            demands.get(i).setRecruitname(rename);
            int roieid=demands.get(i).getRoieid();
            if (roieid!=-1){
                String roname=roleService.findById(roieid).getDescription();
                demands.get(i).setRolename(roname);
            }
        }
        //查询总条数(该名称下)
        map.put("count", demands.size());
        map.put("data", demands);
        return map;
    }

    /**
     * 添加需求
     */
    @GetMapping(path = "/insert")
    public ModelAndView insert(HttpSession session) {
        ModelAndView mav = new ModelAndView("demand/insert");
        //全部部门
        //招聘负责人
       List<Department> departmentList=departmentService.findAll();
        //获取当前登陆人的编号
        Employee employeeInfo=(Employee)session.getAttribute("loginUser");
        //登陆人id
        int eid=employeeInfo.getEmpId();
        EmployeeInfo employeeI=employeeInfoService.findEmployeeInfoById(eid);
       mav.addObject("ename",employeeI);
        mav.addObject("departmentList",departmentList);
        return mav;
    }

    @PostMapping(path = "ajaxone")
    @ResponseBody
    public Map<String, Object> ajaxone(int id){//部门id
        Map<String,Object> map=new HashMap<String,Object>();
        //根据部门id查询部门经理
        int did=departmentService.findById(id).getDperson();//did为该部门的经理
        //根据did查询员工表
        EmployeeInfo empname=employeeInfoService.findEmployeeInfoById(did);
        //根据部门编号,查询该部门下的所有角色
        List<Role> roles= roleService.roels(id);
        map.put("empname",empname);
        map.put("roles",roles);
        return map;
    }

    @PostMapping(path = "/insertdemand")
    @ResponseBody
    @Transactional(rollbackFor=Exception.class)
    public Map<String,Object> insertdemand(Demand demand){
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("code", 0);
        if (demand.getRoleadd()==null || demand.getRoleadd()==""){
            demand.setRoleadd("");
        }else{
            Role r=new Role();
            String rolename=demand.getRoleadd();
            r.setDescription(rolename);
            r.setRole(employeeInfoServiceimpl.toPinyin(rolename));
            r.setDepartmentId(demand.getDeparid());
            roleService.addRole(r);
        }
        int resule= demandService.insertDem(demand);
        if (resule>0){
            map.put("msg","添加成功");
            map.put("data","success");
        }else {
            map.put("msg","添加失败");
            map.put("data","fail");
        }
        return map;
    }

    /**
     * 获取要修改的信息
     * @param id
     * @return
     */
    @GetMapping(path = "/updemand")
    public ModelAndView updemand(int id) {
        ModelAndView mav = new ModelAndView("demand/edit");
        Demand demand= demandService.findByid(id);
        mav.addObject("demand",demand);
        return mav;
    }

    @PostMapping(path = "/update")
    @ResponseBody
    public Map<String,Object> update(Demand demand){
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("code", 0);
        int resule= demandService.updateDem(demand);
        if (resule>0){
            map.put("msg","修改成功");
            map.put("data","success");
        }else {
            map.put("msg","修改失败");
            map.put("data","fail");
        }
        return map;
    }
    /**
     * 填写面试申请
     */
    @GetMapping(path = "/stinsert")
    public ModelAndView stinsert(int id) {
        ModelAndView mav = new ModelAndView("demand/stinsert");
        mav.addObject("id",id);
        return mav;
    }

    @PostMapping(path = "/insertsp")
    @ResponseBody
    @Transactional(rollbackFor=Exception.class)
    public Map<String,Object> insertsp(Interview interview){
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("code", 0);
        //获取需求表的内容
        Demand demand=demandService.findByid(interview.getDemandid());
        int mianshi=demand.getDemand();
        interview.setEmpid(mianshi);
        interview.setStateid(10);
        int resule= interviewService.insertDem(interview);
        if (resule>0){
            map.put("msg","申请成功");
            map.put("data","success");
        }else {
            map.put("msg","申请失败");
            map.put("data","fail");
        }
        return map;
    }
}
